package com.training.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.StringUtils;
import com.training.admin.dao.ProjectTraineeDao;
import com.training.admin.dao.TrainingResultDao;
import com.training.admin.entity.project.ProjectPhase;
import com.training.admin.entity.project.ProjectTrainee;
import com.training.admin.entity.project.TrainingResult;
import com.training.admin.service.DataService;
import com.training.admin.service.ProjectTraineeResultService;

/**
 * 
 * @author sjh
 *
 */
@Service("projectTraineeResultService")
public class ProjectTraineeResultServiceImpl implements ProjectTraineeResultService{

	@Dao
	private ProjectTraineeDao projectTraineeDao;
	
	@Dao
	private TrainingResultDao trainingResultDao;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<ProjectPhase> projectPhaseService;
	
	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.ProjectTraineeResultService#listTrainee(java.lang.String)
	 */
	public List<Map<String, String>> listProjectTrainee(String query, Long projectPhaseId) {
		String search = StringUtils.connect("%", query, "%");
		
		ProjectPhase projectPhase = projectPhaseService.load(ProjectPhase.class, projectPhaseId);
		
		 List<ProjectTrainee> projectTrainees = projectTraineeDao.listBySearch(search, search, search, projectPhase.getProject().getId());
		
		Map<String, ProjectTrainee> filterTrainees = new HashMap<String, ProjectTrainee>();
       
		List<Map<String, String>> listMap = new ArrayList<>();
       
		for(ProjectTrainee projectTrainee : projectTrainees) {
          
			if(filterTrainees.get(projectTrainee.getTrainee().getId().toString()) == null) {
              
				Map<String, String> map = new HashMap<String,String>();
                map.put("id", projectTrainee.getTrainee().getId().toString());
                map.put("text", projectTrainee.getTrainee().getName() + "-" + projectTrainee.getTrainee().getDepartmentName());
                listMap.add(map);
         
			}
        }
		
		 return listMap;
	}

	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.ProjectTraineeResultService#loadByTraineeId(java.lang.String)
	 */
	public TrainingResult loadByTraineeId(Long query,Long projectPhaseId) {
		
		return trainingResultDao.loadByTraineeAndProjectPhase(query, projectPhaseId);
		
	}

}
